package com.my.screenadapter.MPerMission;

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Build;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.text.TextUtils;

import com.my.screenadapter.Base.BaseActivity;
import com.my.screenadapter.PermissionListererInterface;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * Created by 张英杰 on 2017/3/17.
 * 测试在 非activity中如何 申请权限
 */

public class Util {

    public static void  test() {
        BaseActivity.requestRuntimePermission(new String[]{
                Manifest.permission.CALL_PHONE
        }, new PermissionListererInterface() {
            @Override
            public void onGranted(List<String> grantedPermission) {

            }

            @Override
            public void onDenied(List<String> deniedPermission) {

            }
        });
    }


    //请求权限的设置
    public static void requestRuntimePermission(Activity context, Map<String,String> map, PermissionListener listener) {
//        List<String> permissionList = new ArrayList<>();
//        for (String permission : permissions) {
//            if (ContextCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
//                permissionList.add(permission);
//            }
//        }
        HashMap<String,String> noGrantedMap=new HashMap<>();
        Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator();
        while (iterator.hasNext()){
            Map.Entry<String, String> next = iterator.next();
            String permission = next.getKey();
            String value = next.getValue();
            if (!TextUtils.isEmpty(permission)&&!TextUtils.isEmpty(value)){
                if (ContextCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
                    noGrantedMap.put(permission,value);
                }
            }
        }

        if (noGrantedMap.size()>0) {
            boolean isAllNoGranted = false;
            Iterator<Map.Entry<String, String>> iterator2 = noGrantedMap.entrySet().iterator();
            StringBuffer noticeNames=new StringBuffer();
            String[]permissionList =new String[noGrantedMap.size()];
            int indexStr=0;
            while (iterator2.hasNext()){
                Map.Entry<String, String> next = iterator2.next();
                String permission = next.getKey();
                String value = next.getValue();
                if (!TextUtils.isEmpty(permission)&&!TextUtils.isEmpty(value)){
                    boolean isTip = ActivityCompat.shouldShowRequestPermissionRationale(context, permission);
                    permissionList[indexStr]=permission;
                    indexStr++;
                    if (!isTip){
                        isAllNoGranted=true;//用户拒绝永久不弹出提示
                    }
                    noticeNames.append(value);
                    noticeNames.append(" ");
                }
            }

            if (!isAllNoGranted) {// 表明用户没有彻底禁止弹出权限请求
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {//Android6.0（SDK23）
                    ActivityCompat.requestPermissions(context, permissionList, 1);
                } else {
                    // 这里一般会提示用户进入权限设置界面
                    AppSetting.showNoticeDialog(context, "设置", "请打开"+noticeNames.toString());
                }
            } else {// 表明用户已经彻底禁止弹出权限请求
                // 这里一般会提示用户进入权限设置界面
                AppSetting.showNoticeDialog(context, "设置", "请打开"+noticeNames.toString());
//                ActivityCompat.requestPermissions(context,new String[]{"android.permission.WRITE_EXTERNAL_STORAGE"},1231);
            }

        } else {
            listener.onGranted();
        }
    }
}
